<?php
class Core_Model_Http_Request {

	/**
	 * Scheme for http
	 */
	const SCHEME_HTTP  = 'http';

	/**
	 * Scheme for https
	 */
	const SCHEME_HTTPS = 'https';

	/**
	 * Everything in REQUEST_URI before PATH_INFO and SCRIPT_NAME
	 * <form action="<?=$baseUrl?>/upload.php"/>
	 *
	 * @return string
	 */
	public function getBaseUrl() {
		$scheme = $this->getScheme();;
		$name = $this->getServer('SERVER_NAME');
		$port = $this->getServer('SERVER_PORT');
		$h = ($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443) ?
			$name : $name . ':' . $port;
		$base = $this->getBasePath();
		return $scheme . "://" . $h . $base;
	}

	/**
	 * Everything in REQUEST_URI before PATH_INFO not including the filename
	 * <img src="<?=$basePath?>/images/pic.png"/>
	 *
	 * @return string
	 */
	public function getBasePath() {
		return dirname($this->getServer('SCRIPT_NAME'));
	}

	/**
	 * Returns everything between the BaseUrl and QueryString.
	 * This value is generated from the PATH_INFO in $_SERVER
	 * be warning to cross-platform differences.
	 *
	 * @return string
	 */
	public function getPathInfo() {
		return $this->getServer('PATH_INFO');
	}

	/**
	 * Retrieve a member of the $_GET superglobal
	 *
	 * If no $key is passed, returns the entire $_GET array.
	 *
	 * @param string $key
	 * @return mixed Returns null if key does not exist
	 */
	public function getQuery($key = null) {
		if (null === $key) return $_GET;
		return (isset($_GET[$key])) ? $_GET[$key] : null;
	}

	/**
	 * Retrieve a member of the $_POST superglobal
	 *
	 * If no $key is passed, returns the entire $_POST array.
	 *
	 * @todo How to retrieve from nested arrays
	 * @param string $key
	 * @return mixed Returns null if key does not exist
	 */
	public function getPost($key = null) {
		if (null === $key) {
			return $_POST;
		}

		return (isset($_POST[$key])) ? $_POST[$key] : null;
	}

	/**
	 * Retrieve a member of the $_SERVER superglobal
	 *
	 * If no $key is passed, returns the entire $_SERVER array.
	 *
	 * @param string $key
	 * @return mixed Returns null if key does not exist
	 */
	public function getServer($key = null)	{
		if (null === $key) {
			return $_SERVER;
		}

		return (isset($_SERVER[$key])) ? $_SERVER[$key] : null;
	}

	/**
	 * Get the request URI scheme
	 *
	 * @return string
	 */
	public function getScheme() {
		return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
	}

	public function isSecure() {
		return ($this->getScheme() === self::SCHEME_HTTPS);
	}

	/**
	 * Return the method by which the request was made
	 *
	 * @return string
	 */
	public function getMethod() {
		return $this->getServer('REQUEST_METHOD');
	}

	/**
	 * Was the request made by POST?
	 *
	 * @return boolean
	 */
	public function isPost() {
		if ('POST' == $this->getMethod()) {
			return true;
		}

		return false;
	}

	/**
	 * Was the request made by GET?
	 *
	 * @return boolean
	 */
	public function isGet() {
		if ('GET' == $this->getMethod()) {
			return true;
		}

		return false;
	}
}